//
//  WHZScrollView.m
//  WHZScrollerView
//
//  Created by xalo on 16/6/22.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "WHZScrollView.h"

#define kWidthFame self.frame.size.width
#define kHeightFame self.frame.size.height
@interface WHZScrollView ()<UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIImageView *leftImageView;
@property (nonatomic,strong)UIImageView *rightImageView;
@property (nonatomic,strong)NSArray *imageArray;
@property (nonatomic,assign)NSInteger index; //当前下标
//小圆点
@property (nonatomic,strong)UIPageControl *pageControl;
//定时器
@property (nonatomic,strong)NSTimer *timer;
@end
@implementation WHZScrollView

- (NSTimer *)timer{
    if (!_timer) {
        _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        
    }
    return _timer;
}
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        
        //创建滚动视图,及属性
        _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidthFame, kHeightFame)];
        _scrollView.contentSize=CGSizeMake(kWidthFame*2, kHeightFame);
       _scrollView.pagingEnabled=YES;
        _scrollView.bounces=NO;
        _scrollView.bouncesZoom=NO;
        _scrollView.showsHorizontalScrollIndicator=NO;
        _scrollView.delegate=self;
    }
    return _scrollView;
}

- (UIPageControl *)pageControl{
    if (!_pageControl) {
        
        //创建小圆点
        _pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, kHeightFame-50, self.frame.size.width, 50)];
        _pageControl.pageIndicatorTintColor=[UIColor blackColor];
        _pageControl.currentPageIndicatorTintColor=[UIColor redColor];
        _pageControl.numberOfPages=self.imageArray.count;
    }
    return _pageControl;
}

- (UIImageView *)leftImageView{
    if (!_leftImageView) {
        
        _leftImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidthFame, kHeightFame)];
        _leftImageView.backgroundColor=[UIColor redColor];
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];
    }
    return _leftImageView;
}

- (UIImageView *)rightImageView{
    if (!_rightImageView) {
    
        _rightImageView=[[UIImageView alloc]initWithFrame:CGRectMake(kWidthFame, 0, kWidthFame, kHeightFame)];
        _rightImageView.backgroundColor=[UIColor purpleColor];
       [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
       
    }
    return _rightImageView;
}
- (void)timerAction{
    
    [self.scrollView setContentOffset:CGPointMake(kWidthFame, 0) animated:YES];
    
}


- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray{
    
    self=[super initWithFrame:frame];
    if (self) {
        
        self.index=1;
        //给数组赋值
        self.imageArray=imageArray;
        
        //设置视图
        [self createUI];
        
        }
    return self;
}

- (void)createUI{
    
    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.rightImageView];
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];
    
    //延迟线程
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.timer fire];
    });

}
//协议
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
  
    if (scrollView.contentOffset.x==kWidthFame) {
        scrollView.contentOffset=CGPointZero;//CGPointMake(0,0)
    
        
        self.pageControl.currentPage=self.index;
        self.index++;
        if (self.index==self.imageArray.count) {
            self.index=0;
        }
        self.leftImageView.image=self.rightImageView.image;
         [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
    }
}




@end
